package com.lancoo.klgcourseware.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by pengmi on 2017/11/19.
 */

public class KlgTimeUtils {

    public static String getTimeMinAndSecond(int time){
        if(time<60){
            return "00:"+(time<10?"0":"")+time;
        }else{
            int secound=time%60;
            int min=time/60;
            return  (min<10?"0":"")+min+":"+(secound<10?"0":"")+secound;
        }
    }

    public static String getTimeStringToDay(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
        Date curDate=new Date(System.currentTimeMillis());
        return sdf.format(curDate);
    }
    public static String getTimeString(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.CHINA);
        Date curDate=new Date(System.currentTimeMillis());
        return sdf.format(curDate);
    }
    public static String getTimeStringAll(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
        Date curDate=new Date(System.currentTimeMillis());
        return sdf.format(curDate);
    }
    public static String getTimeHourAndMin(){
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm",Locale.CHINA);
        Date curDate=new Date(System.currentTimeMillis());
        return sdf.format(curDate);
    }

    /**
     * 比较两个时间的大小 yyyy-MM-dd hh:mm
     */
    public static boolean compareTime(String firstTime,String secondTime){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        try{
            Date date1 = df.parse(firstTime);
            Date date2 = df.parse(secondTime);
            return  date1.getTime()<date2.getTime();
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 获取当前的时间
     */
    public static long getCurrentTimeMillisecond(){
        return   System.currentTimeMillis();
    }

    /**
     *
     * @param time yyyy-MM-dd HH:mm:ss
     * @return  时间对应的毫秒数
     */
    public static long getDateMilliSecond(String time){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
        try {
            Date parse = sdf.parse(time);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return getCurrentTimeMillisecond();
        }
    }

    /**
     *
     * @param dayAgoTime 过去的时间 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getTimeDayAgo(String dayAgoTime){
        //当前时间的毫秒数
        long currentTimeMillisecond = getCurrentTimeMillisecond();
        //传入时间的毫秒数
        long dateMilliSecond = getDateMilliSecond(dayAgoTime);
        long subTime=currentTimeMillisecond-dateMilliSecond;
        if(subTime>0){
            int day= (int) (subTime/1000/60/60/24);
            if(day>0){
                return ""+day+"天前";
            }else {
                return "一天内";
            }
        }else{
            return "一天内";
        }

    }

    /**
     * 比较两个时间的大小 HH:mm:ss
     */
    public static boolean compareTimeForHHmmss(String time1,String time2){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm:ss",Locale.CHINA);
        try {
            Date parse1 = simpleDateFormat.parse(time1);
            Date parse2 = simpleDateFormat.parse(time2);
            return parse1.getTime()<parse2.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }
    /**
     * 比较两个时间的大小 HH:mm
     */
    public static boolean compareTimeForHHmm(String time1,String time2){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm",Locale.CHINA);
        try {
            Date parse1 = simpleDateFormat.parse(time1);
            Date parse2 = simpleDateFormat.parse(time2);
            return parse1.getTime()<parse2.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }


    /**
     * 比较两个时间的大小 HH:mm
     */
    public static boolean compareTimeForHHmmInHalfHour(String time1,String time2){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm",Locale.CHINA);
        try {
            Date parse1 = simpleDateFormat.parse(time1);
            Date parse2 = simpleDateFormat.parse(time2);
            return parse1.getTime()-parse2.getTime()<1000*60*30;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }

    public static String changeTime(long time){
        Date date=new Date(time);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return format.format(date);
    }

    public static String getDays(String startDate,String endDate){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date startTime = format.parse(startDate);
            Date endTime = format.parse(endDate);
            long subTime=endTime.getTime()- startTime.getTime();
            int days= (int) (subTime/1000/60/60/24);

            if(days>0){
                return days+"天";
            }else{
                int hour=(int) (subTime/1000/60/60);
                if(hour>0){
                    return hour+"小时";
                }else{
                  int min= (int) (subTime/1000/60);
                    return min+"分钟";
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "1天";
        }
    }

    public static String getHourAndMinuteForMin(int min){
        if(min<60){
            return "00:"+(min<10?"0":"")+min+"";
        }else{
            int hour=min/60;
            int minute=min%60;
            return (hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute+"";
        }

    }

    public static String getTimeStringAllNoFuhao(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss",Locale.CHINA);
        Date curDate=new Date(System.currentTimeMillis());
        return sdf.format(curDate);
    }


    public static String getMinAnSecondForSecond(int second){
        if(second<60){
            return second+"秒";
        }else {
            int min=second/60;
            int sec=second%60;
            return min+"分"+(sec!=0?(sec+"秒"):"");
        }

    }

    public static String getStudyTime(int second){
        if(second<3600){
            return getTimeMinAndSecond(second);
        }else{
            int hour=second/3600;
            return (hour<10?"0":"")+hour+":"+getTimeMinAndSecond(second%3600);
        }
    }


}
